package com.letoken.platform.repo.ecommerce.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.app.req.mall.EvaluateListReq;
import com.letoken.platform.repo.app.res.mall.EvaluateListRes;
import com.letoken.platform.repo.ecommerce.dto.ShopProductReq;
import com.letoken.platform.repo.ecommerce.dto.UserCollectListReq;
import com.letoken.platform.repo.ecommerce.dto.UserFootprintReq;
import com.letoken.platform.repo.ecommerce.dto.UserProductReq;
import com.letoken.platform.repo.ecommerce.po.EcommerceProduct;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.letoken.platform.repo.ecommerce.po.EcommerceProductSku;
import com.letoken.platform.repo.ecommerce.po.EcommerceUserAddress;
import com.letoken.platform.repo.ecommerce.vo.ShopProductRes;
import com.letoken.platform.repo.ecommerce.vo.StoreProductRes;
import com.letoken.platform.repo.ecommerce.vo.UserProductDetailRes;
import com.letoken.platform.repo.ecommerce.vo.UserProductRes;
import com.letoken.platform.repo.ecommerce.vo.*;
import com.letoken.platform.repo.mall.po.AppMallInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 商品 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2023-09-08
 */
public interface EcommerceProductMapper extends BaseMapper<EcommerceProduct> {


    @Select("select  product_id as id, (sum(score) / count(1)) as score from ecommerce_user_evaluate where state = 0 group by product_id ")
    List<EcommerceProduct> caulScore();



    @Select("select product_id from ecommerce_user_order where id = #{orderId}")
    Integer selectIdByOrderId(@Param("orderId") Integer orderId);

    @Update("update ecommerce_product set hits = hits + #{addHit}  where id = #{id} limit 1")
    int updateHits(@Param("id") Integer id , @Param("addHit") Integer addHit);


    @Select("select mall_id from ecommerce_product where id = #{id} and publish_status = 0")
    Integer selectMallIdByProductId(@Param("id") Integer id);


    @Select("select mall_id from ecommerce_product where id = #{id} ")
    Integer selectMallIdByProductIdIgnoreStatus(@Param("id") Integer id);



    @Select("select * from ecommerce_product where id = #{id} and mall_id = #{shopId}")
    EcommerceProduct selectByShopId(@Param("id")Integer id , @Param("shopId") Integer shopId);

    Page<UserProductRes> userProductList(@Param("page") Page<UserProductRes> page , @Param("req") UserProductReq req);

    Page<UserFootprintRes> footprints(@Param("page") Page<UserFootprintRes> page , @Param("req") UserFootprintReq req);


    Page<UserCollectListRes> collectList(@Param("page") Page<UserCollectListRes> page , @Param("req") UserCollectListReq req);

    Page<ShopProductRes> msgProductList(@Param("page") Page<ShopProductRes> page , @Param("req") ShopProductReq req);

    Page<ShopVideoProductRes> videoProducts(@Param("page") Page<ShopVideoProductRes> page , @Param("req") ShopProductReq req);



    @Select("SELECT p.publish_status, p.contributing, pd.assistant_title, p.mall_id, p.id,p.pic,p.tags,pd.album_pics albumPics,pd.graphic_details graphicDetails,p.price, p.title,p.real_price ," +
            "(select state from ecommerce_product_collect where product_id = #{productId} and user_id = #{userId} ) as collectState " +
            " FROM ecommerce_product p INNER JOIN  ecommerce_product_detail pd ON p.id = pd.id  WHERE p.id = #{productId}")
    UserProductDetailRes productDetailByProductId(@Param("productId") Integer productId,@Param("userId") Integer userId);


    @Select("SELECT * FROM ecommerce_product_sku WHERE product_id = #{productId} and delete_status = 0  order by price asc limit 1")
    EcommerceProductSku defaultSpecification(@Param("productId") Integer productId);


    @Select("SELECT * FROM ecommerce_user_address WHERE user_id = #{userId} and default_status = 1 and type = 0  and delete_status = 0 order by id desc limit 1")
    EcommerceUserAddress defaultAddress(@Param("userId") Integer userId);



    @Select("SELECT * FROM ecommerce_user_address WHERE user_id = #{userId} and delete_status = 0 order by update_time desc limit 1")
    EcommerceUserAddress fistAddress(@Param("userId") Integer userId);


    Page<StoreProductRes> products(@Param("page") Page<ShopProductRes> page , @Param("req") ShopProductReq req);

    @Select("SELECT  ep.publish_status, ep.id,eps.id as skuId, ep.title ,ep.pic ,ep.tags ,ep.contributing,eps.sp_data, eps.real_price ,eps.stock FROM  ecommerce_product_sku eps  inner join ecommerce_product ep on eps.product_id  = ep.id where eps.id in (${list})")
    List<UserShoppingCartListRes> selectShoppingCart(@Param("list") String skuIds);


    @Select("select ep.id,ep.mall_id , (SELECT  a.shipping_type  FROM  app_mall_info a where a.id = ep.mall_id) as shipping_type  from ecommerce_product ep  where ep.id  in (${list}) ")
    List<ShippingCenterVo> shippingTypesAndProduct(@Param("list") String ids);



    void updateBatchScoreById(@Param("list") List<EcommerceProduct> list);

    @Select("select 1 from ecommerce_product_sku where product_id = #{productId} and id = #{skuId} limit 1 ")
    Integer productSkuExists(@Param("productId")Integer productId , @Param("skuId") Integer skuId);

}
